package com.pactera.asmp.server;

import com.pactera.asmp.server.common.logprint.aspect.PluginLogAspect;

import com.pactera.asmp.server.utils.AsmpAuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

import java.util.TimeZone;

import javax.annotation.PostConstruct;

@Configuration
@SpringBootApplication()
@EnableAspectJAutoProxy(proxyTargetClass=true)
@EnableAsync
@Slf4j
public class AsmpserverApplication {

    public static ConfigurableApplicationContext configurableApplicationContext;

    public static void main(String[] args) {
        if(!AsmpAuthUtil.verifyAuth()){
            log.error("verify auth result is false!!!");
//            return;
        }
        SpringApplication springApplication = new SpringApplication(AsmpserverApplication.class);
        System.setProperty("spring.devtools.restart.enabled", "false");
        System.setProperty("mail.mime.splitlongparameters", "false");
        configurableApplicationContext = springApplication.run(args);
    }

    @PostConstruct
    void started() {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
    }

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(1024*1024*10);
        container.setMaxBinaryMessageBufferSize(1024*1024*20);
        return container;
    }

    @Bean(name="pluginLogAspect")
    public PluginLogAspect initPluginAspect(){
        return new PluginLogAspect();
    }
}
